import { ShareDefine } from "../../../Game/Scripts/Config/ShareDefine";
import NetManager from "../Net/NetManager";
import ComTool from "./ComTool";
import ConfirmManager from "./ConfirmManager";
import EventMgr from "./EventMgr";

const {ccclass, property} = cc._decorator;

@ccclass
export default class SysNotifyManager extends cc.Component {
    public static Instance: SysNotifyManager = null;
    regExpDict: {};
    ShareDefine: any;
    ComTool: any;
    NewSysMsg: any;
    onLoad() {
        if (SysNotifyManager.Instance !== null) {
            this.destroy();
            return;
        }
        SysNotifyManager.Instance = this;
    }

    
	/**
	 * 初始化
	 */
	Init(){

		this.SysDataManager = SysDataManager();
		this.NewSysMsg = this.SysDataManager.GetTableDict("NewSysMsg");

		this.ComTool = ComTool.Instance;
		this.ShareDefine = ShareDefine;

		//缓存的正则表达式字典
		this.regExpDict = {};

		NetManager.Instance.RegNetPack("S1010_SystemMessage", this.OnPack_ServerSysMsg, this);

		console.log("create SysNotifyManager");
	}


	//-----------------------回调函数-----------------------------
	/**
	 * 接收封包
	 */
	OnPack_ServerSysMsg(serverPack){
		//系统提示码
		let msgID = serverPack.key;
		let argList = serverPack.p||[];

		let count = argList.length;
		let msgArgList = [];
		for(let index=0; index<count; index++){

			let data = argList[index];

			if(data["StringValue"]){
				msgArgList.push(data["StringValue"]);
			}
			else{
				msgArgList.push(data["IntValue"]);
			}
		}

		this.ShowSysMsg(msgID, msgArgList);

		EventMgr.Instance.Emit("ServerSysMsg", {"MsgID":msgID, "MsgArgList":msgArgList});
	}

	//-----------------------获取函数-----------------------------

	//获取系统提示文本
	GetSysMsgContentByMsgID(msgID, paramList = null){

		if(!paramList){
			paramList = [];
		}
		//如果查找不到系统提示码,则为纯文本弹框提示直接播放不解码
		if(!this.NewSysMsg.hasOwnProperty(msgID)){
			console.log("GetSysMsgContentByMsgID(%s) NewSysMsg.txt not find", msgID);
			return msgID;
		}
		let msgInfo = this.NewSysMsg[msgID];
		let msgContent = msgInfo['Content'];

		return this.TranslateMsgContent(msgContent, paramList);

	}

	/**
	 * 替换消息文本
	 * @param msgID 消息ID
	 * @return 无返回值
	 * @remarks 
	 */
	TranslateMsgContent(msgContent, paramList){

		let count = paramList.length;

		// 替换文本
		for(let index=0; index<=count; index++){

			let param = paramList[index];
			let argIndex = index + 1;
			let regExpObj = null;


			let regKeyString = this.ComTool.StringAddNumSuffix("S", argIndex, 1);
			if(msgContent.indexOf(regKeyString) != -1){

				if(this.regExpDict.hasOwnProperty(regKeyString)){
					regExpObj = this.regExpDict[regKeyString];
				}
				else{
					regExpObj = new RegExp("{" + regKeyString + "}", "g");
					this.regExpDict[regKeyString] = regExpObj
				}
				msgContent = msgContent.replace(regExpObj, param);
			}
		}

		return msgContent
	}
	
	/**
	 * 显示系统提示
	 * @param msgID
	 */
	ShowSysMsg(msgID, msgArgList=[], msgPos = 4){
		if (typeof(msgID) == "undefined") {
			console.log("弹窗提示内容为空");
			return;
		}
		console.log("ShowSysMsg(%s):", msgID, msgArgList);

		let msgContent = "";
		//如果查找不到系统提示码,则为纯文本弹框提示直接播放不解码
		if(this.NewSysMsg.hasOwnProperty(msgID)){
			let msgInfo = this.NewSysMsg[msgID];
			msgPos = msgInfo["Pos"];

			msgContent = msgInfo['Content'];
			msgContent = this.TranslateMsgContent(msgContent, msgArgList);
		}
		else{
			// msgPos = 4;
			msgContent = msgID;
		}


		//出现位置 1系统广播(不允许主动调用这种类型,只能又聊天系统频道下发)
		if(msgPos == 1){
			console.log("ShowSysMsg(%s) error", msgID);
		}
		//只触发事件不显示提示,由事件自己决定(服务端下发客户端弹2次确认框等待点击)
		else if(msgPos == 2){

		}
		//浮动提示
		else if(msgPos == 3){
			app.FormManager().ShowForm("UIMessage_Drift", msgID, msgArgList, msgContent);
		}
		//"确定" 弹框提示
		else if(msgPos == 4){
			app.FormManager().ShowForm("UIMessage", "OnSys", this.ShareDefine.ConfirmOK, msgID, msgArgList, msgContent);

		}
		//"是","否"" 弹框提示
		else if(msgPos == 5){
			ConfirmManager.Instance.SetWaitForConfirmForm(this.OnConFirm.bind(this), msgID, []);
			ConfirmManager.Instance.ShowConfirm(this.ShareDefine.ConfirmYN, msgID, msgArgList)
		}
		else{
			console.log("ShowSysMsg(%s) msgPos(%s) error", msgID, msgPos);
		}
	}

    OnConFirm(clickType, msgID, backArgList){
    	if(clickType == "Sure"){
    		console.log("打开客服QQ");
    	}
    }
}
